package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.SignInService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SaveAccountServlet extends HttpServlet {

    private Account account;
    private String username;
    private String password;
    private String email;
    private String firstName;
    private String lastName;
    private String status;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String phone;
    private String favouriteCategoryId;
    private String languagePreference;
    private String listOption;
    private String bannerOption;
    private String bannerName;

    private static final String EDIT_ACCOUNT ="/WEB-INF/jsp/account/EditAccountForm.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        account = (Account)session.getAttribute("account");

        username = account.getUsername();

        password = request.getParameter("password");
        email = request.getParameter("email");
        firstName = request.getParameter("firstName");
        lastName = request.getParameter("lastName");
        status = request.getParameter("status");
        address1 = request.getParameter("address1");
        address2 = request.getParameter("address2");
        city = request.getParameter("city");
        state = request.getParameter("state");
        zip = request.getParameter("zip");
        country = request.getParameter("country");
        phone = request.getParameter("phone");
        favouriteCategoryId = request.getParameter("favouriteCategoryId");
        languagePreference = request.getParameter("languagePreference");
        listOption = request.getParameter("listOption");
        bannerOption = request.getParameter("bannerOption");
        bannerName = request.getParameter("bannerName");

        account.setUsername(username);
        account.setPassword(password);
        account.setEmail(email);
        account.setFirstName(firstName);
        account.setLastName(lastName);
        account.setStatus(status);
        account.setAddress1(address1);
        account.setAddress2(address2);
        account.setCity(city);
        account.setState(state);
        account.setZip(zip);
        account.setCountry(country);
        account.setPhone(phone);
        account.setFavouriteCategoryId(favouriteCategoryId);
        account.setLanguagePreference(languagePreference);
        account.setListOption(Boolean.parseBoolean(listOption));
        account.setBannerOption(Boolean.parseBoolean(bannerOption));
        account.setBannerName(bannerName);

        SignInService signInService = new SignInService();
        signInService.updateAccount(account);

        session.setAttribute("account",account);

        if(account != null){
            HttpServletRequest httpRequest= request;
            String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + " Account Changed";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        request.getRequestDispatcher(EDIT_ACCOUNT).forward(request,response);


    }
}
